package cc.kzc.adminauth.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class CustomAsyncConfig implements AsyncConfigurer {

	@Bean("customExecutor")
	public ThreadPoolTaskExecutor asyncOperationExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// 设置核心线程数
		executor.setCorePoolSize(8);
		// 设置最大线程数
		executor.setMaxPoolSize(100);
		// 设置队列大小
		executor.setQueueCapacity(Integer.MAX_VALUE);
		// 设置线程活跃时间(秒)
		executor.setKeepAliveSeconds(60);
		// 设置线程名前缀+分组名称
		executor.setThreadNamePrefix("AsyncOperationThread-");
		executor.setThreadGroupName("AsyncOperationGroup");
		// 所有任务结束后关闭线程池
		executor.setWaitForTasksToCompleteOnShutdown(true);
		// 初始化
		executor.initialize();
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return new CustomAsyncExceptionHandler();
	}

}
